$(document).ready(function () {

    InitBinding();
});


function InitBinding() {


    SwitchHightlighting($("#HighlightingCSSToUse").val());

    $(".snippetActionArea").tooltip({ effect: 'slide' });

    if ($("#WasAddOrEdit").val() == "True") {
        showOkDialog('Your added snippet should be shown below', 180, 'Information');
    }

    $(".collapse img").click(function () {
        $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet").fadeOut(600);
    });

    $(".expand img").click(function () {
        $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet").fadeIn(600);
    });


    $(".tweet img").click(function () {
        var snippetElement = $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet");
        var codeSnippetId = snippetElement.find(".codeSnippetId").val();


        if (!window.location.origin) {
            window.location.origin = window.location.protocol + '//' + window.location.host + '/';
        }

        var tweet = 'https://twitter.com/intent/tweet?source=webclient&text=Currently+viewing+this+code+snippet+' +
            window.location.origin + '/Readonly/Display/' + codeSnippetId;

        // Now, open up the window
        window.open(tweet);
    });


    $(".link img").click(function () {

        var snippetElement = $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet");
        var codeSnippetId = snippetElement.find(".codeSnippetId").val();


        if (!window.location.origin) {
            window.location.origin = window.location.protocol + '//' + window.location.host + '/';
        }

        showNoButtonsDialog(
                '<p><strong>Snippet link</strong><br/><i>' + window.location.origin +
                '/Readonly/Display/' + codeSnippetId + '</i><br/><br/>' +
                'Copy the link to share it' +
                '</p>', 195, 'Information'
            );
    });


    $(".delete.enabled img").click(function () {


        var displayMode = $('#DisplayMode').val();
        var snippetElement = $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet");
        var isGrouped = snippetElement.find(".isGrouped").val();
        var groupDescription = snippetElement.find(".groupDescription").val();
        var groupId = snippetElement.find(".groupId").val();
        var codeSnippetId = snippetElement.find(".codeSnippetId").val();



        if (isGrouped == "true") {

            showYesNoGroupSnippetDialog("Code snippet " + codeSnippetId + " is part of group '" + groupDescription +
                "'.<br/><br/> Please pick whether to delete just the single selected snippet or ALL snippets in the group",
                300, "Confirm Delete",
                function (data) {
                    DeleteSnippetAndShowRemaining(displayMode, codeSnippetId, groupId, false);
                },
                function (data) {
                    DeleteSnippetAndShowRemaining(displayMode, codeSnippetId, groupId, true);
                }
            );
        }
        else {
            showYesNoDialog('Are you sure you want to delete this snippet?', 180, 'Confirm',
                function () {
                    DeleteSnippetAndShowRemaining(displayMode, codeSnippetId, groupId, false);
                }
            );
        }
    });


    $(".edit.enabled img").click(function () {

        var snippetElement = $(this).closest(".highlightedSnippetHeader").next(".highlightedSnippet");
        var codeSnippetId = snippetElement.find(".codeSnippetId").val();
        window.location.href = '/CodeSnippet/Edit' + '?codeSnippetId=' + codeSnippetId;

    });


    DetermineMessageToShow();


}


function DetermineMessageToShow() {

    if ($('.snippet').length == 0) {
        $('#noSnippetsMessage').show();
        $('#snippetsMessage').hide();
    }
    else {
        $('#noSnippetsMessage').hide();
        $('#snippetsMessage').show();
    }
}


function DeleteSnippetAndShowRemaining(displayMode, snippetId, groupId, deleteAllInGroup) {

    $.post("/CodeSnippet/DeleteSnippetAndShowRemaining",
            {
                'displayMode': displayMode,
                'snippetId': snippetId,
                'groupId': groupId == undefined ? -1 : groupId,
                'deleteAllInGroup': deleteAllInGroup
            },
            function (response) {
                if (response) {
                    if (deleteAllInGroup) {
                        $('.highlightedSnippet .groupId[value=' + groupId + ']').closest('.snippet').fadeOut(600, function () {
                            $(this).remove();
                        });
                        showOkDialog('Sucessfully removed all snippet: ' + snippetId, 180, 'Error', function () {
                            DetermineMessageToShow()
                        });
                    }
                    else {
                        $('#codeSnippet-' + snippetId).fadeOut(600, function () {
                            $(this).remove();
                        });
                        showOkDialog('Sucessfully removed snippet: ' + snippetId, 180, 'Error', function () {
                            DetermineMessageToShow()
                        });
                    }
                }
                else {
                    showOkDialog('Could not delete code snippet', 180, 'Error', function () {
                        DetermineMessageToShow()
                    });
                }
            },
            'json'
    );
}